綜合演練

小明想知道信義區的腳踏車站晴天和雨天的使用率有何差別 提示: - filtermutateselectgroup_bysummarise - dcast - arrange

sna 晴天 雨天
永吉松信路口 0.8720583 0.4744500
信義廣場(台北101) 0.7952094 0.5329188
捷運永春站(2號出口) 0.7888917 0.4466667
市民廣場 0.7369417 0.3238917
捷運台北101/世貿站 0.7294856 0.6448654
基隆光復路口 0.7277750 0.7589000
仁愛逸仙路口 0.6532895 0.5921053
三張犁 0.6409129 0.4242424
五常公園 0.6233278 0.3536944
中強公園 0.6216667 0.7833333



#讀取CSV資料
ubike <- read.csv(file = "./ubike-weather-big5.csv",
          colClasses = c("factor", "integer", "integer", "factor", "factor",
                         "numeric", "numeric", "integer", "numeric", "integer",
                         "integer", "numeric", "numeric", "integer", "integer",
                         "numeric", "numeric", "numeric", "numeric", "numeric",
                         "numeric", "numeric", "numeric"),
          fileEncoding = 'BIG-5',
          stringsAsFactors = F)

引用套件 dplyr:優化使用
reshape2:為使用dcast,fun.aggregate
knitr:表格套件

library(dplyr)
library(reshape2)
library(knitr)
library(ggmap)
library(plotly)
library(ggplot2)

確認資料

str(ubike)
## 'data.frame':    34256 obs. of  23 variables:
##  $ date     : Factor w/ 7 levels "2014-12-08","2014-12-09",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ hour     : int  15 15 15 15 15 15 15 15 15 15 ...
##  $ sno      : int  2 3 4 5 6 7 8 9 10 11 ...
##  $ sarea    : Factor w/ 19 levels "三重區","士林區",..: 4 14 14 14 14 14 14 14 14 14 ...
##  $ sna      : Factor w/ 230 levels "Y-17青少年育樂中心",..: 163 37 41 220 33 89 34 75 36 6 ...
##  $ lat      : num  25 25 25 25 25 ...
##  $ lng      : num  122 122 122 122 122 ...
##  $ tot      : int  48 40 60 60 80 80 60 40 54 66 ...
##  $ avg.sbi  : num  24 10.3 39.3 34.2 31.3 ...
##  $ max.sbi  : int  27 13 40 35 32 25 20 24 17 46 ...
##  $ min.sbi  : int  23 8 38 33 31 23 19 23 14 42 ...
##  $ std.sbi  : num  1.549 1.862 1.033 0.753 0.516 ...
##  $ avg.bemp : num  22 29.7 20.7 25.8 47.7 ...
##  $ max.bemp : int  23 32 22 27 48 57 41 17 40 23 ...
##  $ min.bemp : int  19 27 20 25 47 55 40 16 37 19 ...
##  $ std.bemp : num  2 2 1 1 1 1 1 1 2 1 ...
##  $ temp     : num  16.5 16.2 16.3 16.2 16.2 ...
##  $ max.temp : num  18.9 18.9 18.9 18.9 18.9 ...
##  $ min.temp : num  16.3 16.2 16.3 16.1 16.2 ...
##  $ humidity : num  89.9 91.8 91.6 92.4 92.3 ...
##  $ pressure : num  1021 1020 1020 1020 1020 ...
##  $ max.anemo: num  2.62 2.42 2.61 2.47 2.6 ...
##  $ rainfall : num  14.6 14.3 14.1 14.4 14 ...

資料正規劃

zzzz <- (select(ubike, sarea, sna, rainfall, avg.bemp, tot, lat, lng) %>%
#搜尋信義區
filter(sarea == "信義區") %>%
#判斷是否雨天,降雨為0都為晴天
#但透過View看過資料後發現有NA的資料,加入判斷空值
#using為使用率=空車位/實際車位
mutate(day = ifelse(is.na(rainfall) | rainfall == 0, "晴天", "雨天"), using = avg.bemp / tot) %>%
select(sarea, sna, day, using, tot, lat, lng))

轉換成圖表後,可以看到晴天的租借率大部分都高於雨天

#使用Dcast將晴天雨天的類型座位顯示資料展開
display <- dcast(zzzz, formula = sna ~ day, fun.aggregate = mean, value.var = "using")
#轉換表格模式
kable(display)
sna 雨天 晴天
三張犁 0.6167054 0.6132980
中強公園 0.6439729 0.7270987
五常公園 0.5659657 0.7811830
仁愛逸仙路口 0.6849665 0.4538985
世貿二館 0.5906882 0.9585196
世貿三館 0.5313098 0.4273000
台北市災害應變中心 0.4951956 0.2316852
台北市政府 0.6412121 0.9105188
市民廣場 0.5655888 0.6346306
永吉松信路口 0.5315990 0.7645007
吳興公車總站 0.4584263 0.3017857
松山家商 0.5726507 0.5564038
松德公園 0.5120483 0.6348568
松德站 0.5180784 0.5705405
信義廣場(台北101) 0.6999824 0.7542025
基隆光復路口 0.5773462 0.3542200
捷運台北101/世貿站 0.6670188 0.7591891
捷運市政府站(3號出口) NaN 0.4315784
捷運永春站(2號出口) 0.5370917 0.5205762
捷運象山站 0.6055783 0.6687038
福德公園 0.4913263 0.5705204
臺北醫學大學 0.5408200 0.3556588
興雅國中 0.6623193 0.7029670

將晴天雨天資料個別抽出在地圖上呈現
可以明顯的看到市區的使用率較高,山腳下的使用率較低
唯一較特別的是光復南路雨天的使用率高於晴天

#抽出晴天資料
sun <- filter(zzzz, day == "晴天")
#抽出雨天資料
rain <- filter(zzzz, day == "雨天")
#依作標代抓出GOOGLE MAP信義區地圖
map <- get_map(location = c(lon = 121.570555, lat = 25.032721), zoom = 14)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=25.032721,121.570555&zoom=14&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
#將座標帶入地圖
晴天地圖<-(ggmap(map) +
geom_point(data = sun, aes(x = lng, y = lat, color = using)))
雨天地圖 <- (ggmap(map) +
geom_point(data = rain, aes(x = lng, y = lat, color = using)))
#顯示
晴天地圖

雨天地圖

調整圖案常寬高,但是資料文字仍是太多
使用ggplotly增加資料檢視可是性
資料大多為雨天的資料

xxxxxx <- (ggplot(zzzz, aes(sna, fill = day)) + geom_bar(position = "dodge"))
#使用ggplotly
ggplotly(xxxxxx)